import {findOrder, findOrderStatus} from '@/service'
import {Message,Loading} from "element-ui";

let loading

export default {
  namespaced: true,
  state() {
    return {
      order: [],
      info:[],
      loading: false,
      loadinginfo:false
    }
  },
  getter: {},
  mutations: {
    /**
     * 状态设置器
     * @param state
     * @param {Object} payload  key:建，value:值，isStorage=true同时设置缓存，isStorage=false不设置缓存
     * @constructor
     */
    SET_STATE(state, payload) {
      state[payload.key] = payload.value
      if (payload.isStorage) {
        localStorage.setItem(payload.key, payload.value)
      }
    }
  },


  actions: {
    /**
     * 或缺orderList
     * @param commit
     * @param payload
     * @returns {Promise<never>}
     * @constructor
     */
    async LOAD_LIST({commit}, payload) {
      commit('SET_STATE', {key: 'loading', value: true})
      let source = await findOrder(payload)
      if (source.code === 200) {
        commit('SET_STATE', {key: 'order', value: source.data})
        commit('SET_STATE', {key: 'loading', value: false})
      } else {
        if (source.code !== 401) {
          commit('SET_STATE', {key: 'loading', value: false})
          Message({
            showClose: true,
            message: source.msg,
            type: 'error'
          })
        }
      }
    },
    async GET_INFO({commit}, payload) {
      // var loadingInstance = Loading.service(options);
      loading = Loading.service({
        lock: true,
        text: '加载中……',
        background: 'rgba(0, 0, 0, 0.2)'
      });
      commit('SET_STATE', {key: 'loadinginfo', value: false})

      let source = await findOrderStatus(payload)

      if (source.code === 200) {
        commit('SET_STATE', {key: 'info', value: source.data})
        commit('SET_STATE', {key: 'loadinginfo', value: false})
        // loadingInstance.close();
        loading.close();
      } else {
        if (source.code !== 401) {
          commit('SET_STATE', {key: 'loadinginfo', value: false})
          Message({
            showClose: true,
            message: source.msg,
            type: 'error'
          })
        }
      }


    }
  }
}
